package com.xiaoshuaila.workbench.service;

import com.xiaoshuaila.tool.SqlSessionUtil;
import com.xiaoshuaila.tool.UUIDUtil;
import com.xiaoshuaila.workbench.dao.CustomerDao;
import com.xiaoshuaila.workbench.dao.TranDao;
import com.xiaoshuaila.workbench.dao.TranHistoryDao;
import com.xiaoshuaila.workbench.domain.Customer;
import com.xiaoshuaila.workbench.domain.Tran;
import com.xiaoshuaila.workbench.domain.TranHistory;

import java.util.List;
import java.util.Map;

public class TranserviceImpl implements Transervice {
    private TranDao tranDao=
            SqlSessionUtil.getSqlSession().getMapper(TranDao.class);
    private TranHistoryDao tranHistoryDao=
            SqlSessionUtil.getSqlSession().getMapper(TranHistoryDao.class);
    private CustomerDao customerDao=
            SqlSessionUtil.getSqlSession().getMapper(CustomerDao.class);
    @Override
    public boolean save(Tran tran, String customerName) {
        Customer customer=customerDao.getcustomerbyname(customerName);
        Boolean flag=true;
        if (customer==null)
        {
            customer=new Customer();
            customer.setId(UUIDUtil.getUUID());
            customer.setName(customerName);
            customer.setCreateBy(tran.getCreateBy());
            customer.setCreateTime(tran.getCreateTime());
            customer.setContactSummary(tran.getContactSummary());
            customer.setNextContactTime(tran.getNextContactTime());
            customer.setOwner(tran.getOwner());
            int count= customerDao.save(customer);
            if (count==0)
                flag=false;
        }
        tran.setId(UUIDUtil.getUUID());
        tran.setCustomerId(customer.getId());
        int count2=tranDao.save(tran);
        if (count2==0)
            flag=false;
        TranHistory tranHistory=new TranHistory();
        tranHistory.setId(UUIDUtil.getUUID());
        tranHistory.setTranId(tran.getId());
        tranHistory.setCreateTime(tran.getCreateTime());
        tranHistory.setCreateBy(tran.getCreateBy());
        tranHistory.setStage(tran.getStage());
        tranHistory.setMoney(tran.getMoney());
        tranHistory.setExpectedDate(tran.getExpectedDate());
        int count3=tranHistoryDao.save(tranHistory);
        if (count3==0)
            flag=false;
        return flag;
    }

    @Override
    public Tran detail(String id) {
        return tranDao.datail(id);
    }

    @Override
    public List<TranHistory> findhistorylistbytranid(String id) {

        return tranHistoryDao.findhistorylistbytranid(id);
    }

    @Override
    public Boolean changestage(Tran tran) {
        TranHistory tranHistory=new TranHistory();

        tranHistory.setId(UUIDUtil.getUUID());
        tranHistory.setCreateBy(tran.getCreateBy());
        tranHistory.setCreateTime(tran.getCreateTime());
        tranHistory.setMoney(tran.getMoney());
        tranHistory.setExpectedDate(tran.getExpectedDate());
        tranHistory.setStage(tran.getStage());
        tranHistory.setTranId(tran.getId());
        if (!tranDao.changestage(tran))
            return false;
        if (tranHistoryDao.save(tranHistory)!=1)
        return false;
        return true;
    }

    @Override
    public List<Map> getchaet() {
        return tranDao.getchart();
    }
}
